package com.snowfeel.sfmall.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snowfeel.sfmall.core.constants.EntityConstants;
import com.snowfeel.sfmall.core.enums.ResponseCode;
import com.snowfeel.sfmall.core.exception.BizException;
import com.snowfeel.sfmall.core.vo.PageVo;
import com.snowfeel.sfmall.sys.convert.SysRoleConvert;
import com.snowfeel.sfmall.sys.dto.SysRoleParam;
import com.snowfeel.sfmall.sys.mapper.SysMenuRoleMapper;
import com.snowfeel.sfmall.sys.mapper.SysRoleMapper;
import com.snowfeel.sfmall.sys.model.SysMenuRole;
import com.snowfeel.sfmall.sys.model.SysRole;
import com.snowfeel.sfmall.sys.query.SysRoleQuery;
import com.snowfeel.sfmall.sys.vo.SysPermissionVo;
import com.snowfeel.sfmall.sys.vo.SysRoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 后台角色管理Service实现类
 * Created on 2018/9/30.
 */
@Service
public class SysRoleService {

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysMenuRoleMapper menuRoleMapper;

    @Autowired
    private SysRoleConvert sysRoleConvert;

    public int create(SysRoleParam role) {
        SysRole rolePo = sysRoleConvert.convertPo(role);
        rolePo.setCreateTime(new Date());
        rolePo.setStatus(EntityConstants.YES);
        return roleMapper.insert(rolePo);
    }

    public int update(Long id, SysRoleParam role) {
        SysRole entity = roleMapper.selectById(id);
        if (Objects.isNull(entity)) {
            throw new BizException(ResponseCode.NOT_FOUND);
        }
        role.setId(id);
        SysRole rolePo = sysRoleConvert.convertPo(role);
        return roleMapper.updateById(rolePo);
    }

    public int delete(List<Long> ids) {
        return roleMapper.deleteBatchIds(ids);
    }

    public List<SysPermissionVo> getPermissionList(Long roleId) {
        return menuRoleMapper.getPermissionList(roleId);
    }

    public int updatePermission(Long roleId, List<Long> permissionIds) {
        //先删除原有关系
        LambdaUpdateWrapper<SysMenuRole> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SysMenuRole::getRoleId, roleId);
        menuRoleMapper.delete(wrapper);

        //批量插入新关系
        List<SysMenuRole> relationList = new ArrayList<>();
        for (Long permissionId : permissionIds) {
            SysMenuRole relation = new SysMenuRole();
            relation.setRoleId(roleId);
            relation.setMenuId(permissionId);
            relationList.add(relation);
        }
        return menuRoleMapper.insertBatch(relationList);
    }

    public PageVo<SysRoleVo> page(SysRoleQuery query) {
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(query.getName()), SysRole::getRoleName, query.getName());
        queryWrapper.eq(Objects.nonNull(query.getStatus()), SysRole::getStatus, query.getStatus());
        queryWrapper.orderByDesc(SysRole::getCreateTime);
        IPage<SysRole> page = roleMapper.selectPage(new Page<>(query.getPageNo(), query.getPageSize()), queryWrapper);
        List<SysRoleVo> sysRoleVoList = sysRoleConvert.convertVoList(page.getRecords());
        PageVo<SysRoleVo> pageVO = new PageVo<>();
        pageVO.setPages((int) page.getPages());
        pageVO.setTotal(page.getTotal());
        pageVO.setList(sysRoleVoList);
        return pageVO;
    }
}
